﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Windows.Forms;
namespace RookieLibSharp
{
    public class LibFunction
    {
        public static void WriteBytesToFile(string path, byte[] bytes)
        {
            using (FileStream FS = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                using (BinaryWriter bw = new BinaryWriter(FS))
                {
                    bw.Write(bytes);
                    bw.Close();
                    FS.Close();
                }
            }
        }
        public static byte[] ReadAllBytes(string path)
        {
            if (File.Exists(path))
            {
                return File.ReadAllBytes(path);
            }
            return null;
        }
        public static string GetMatchStr(string strSrc, string pattern)
        {
            Match m = Regex.Match(strSrc, pattern, RegexOptions.IgnoreCase);
            if (m != null)
            {
                return m.Value;
            }
            return "";
        }
        public static string GetNumStrFromStr(string strSrc)
        {
            Match m = Regex.Match(strSrc, @"\d+", RegexOptions.IgnoreCase);
            if (null != m)
            {
                return m.Value;
            }
            return "";
        }
        public static string GetDoubleStrFromStr(string strSrc)
        {
            Match m = Regex.Match(strSrc, @"\d+(\.\d+)?",RegexOptions.IgnoreCase);
            if (null != m)
            {
                return m.Value;
            }
            return "";
        }
        /// <summary>
        /// 新建文件夹
        /// </summary>
        /// <param name="folderPath">文件夹路径</param>
        /// <returns>true：新建成功或者是文件夹存在</returns>
        public static bool CreateFolder(string folderPath)
        {
            //如果文件夹不存在，则新建
            if (!Directory.Exists(folderPath))
            {
                try
                {
                    Directory.CreateDirectory(folderPath);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }
        public static bool DeleteFolder(string folderPath)
        {
            if (Directory.Exists(folderPath))
            {
                try{Directory.Delete(folderPath, true);}
                catch{return false;}
            }
            return true;
        }
        public static bool DeleteFile(string filePath)
        {
            if (File.Exists(filePath))
            {
                try{File.Delete(filePath);}
                catch{return false;}
            }
            return true;
        }
        public static string GetOpenFilePath(string title, string filter)
        {
            string filePath = null;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = title;
            ofd.Multiselect = false;
            ofd.Filter = filter;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                filePath = ofd.FileName;
            }
            ofd = null;
            return filePath;
        }
    }
}
